package cn.jsu.oj.judgment.strategy;



import cn.jsu.oj.judgment.net.netHandler.JudgeClientHandler;
import cn.jsu.oj.judgment.pojo.AbstractPack;
import cn.jsu.oj.judgment.pojo.ApplyTaskNumber;
import cn.jsu.oj.judgment.thread.ApplyTaskThread;
import lombok.extern.slf4j.Slf4j;

import static cn.jsu.oj.judgment.config.DataStructureConfig.taskQueue;
import static cn.jsu.oj.judgment.config.DataStructureConfig.taskQueueCapacity;

/**
 * 唤醒信号包策略处理类
 * typeNo 89
 */
@Slf4j
public class WakeUpHandle implements ApplyRolePackHandle {

    @Override
    public void handle(AbstractPack abstractPack, int ct) throws Exception {
        log.info(">>>>>task count {}",ApplyTaskThread.taskCount);
        int taskNum;
        if (ApplyTaskThread.taskCount < 0) {
            taskNum = taskQueueCapacity + ApplyTaskThread.taskCount - taskQueue.size();
        }
        else {
            taskNum = taskQueueCapacity - ApplyTaskThread.taskCount - taskQueue.size();
        }

        ApplyTaskNumber applyTask = new ApplyTaskNumber((short) taskNum);
        JudgeClientHandler.channel.writeAndFlush(applyTask.serialize());
    }
}
